package wsz.order;

import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.common.message.MessageQueue;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 部分有序：消息发送指定的message queue中
 * @author wsz
 * @date 2022/2/28 10:25
 **/
public class Producer {
    public static void main(String[] args)  throws Exception{
        DefaultMQProducer producer = new DefaultMQProducer("producer_grp_wsz");
        producer.setNamesrvAddr("192.168.174.138:9876");
        producer.start();

        // 获取所有主题的message queue
        List<MessageQueue> messageQueues = producer.fetchPublishMessageQueues("tp_demo_08");
        Message message = null;
        MessageQueue messageQueue = null;

        for (int i = 0; i < 100; i++) {
            // 指定 message queue
            messageQueue = messageQueues.get(i % 8);

            // 业务第一条
            message = new Message(
                    "tp_demo_08",
                    ("tp_demo_08 order create" + i).getBytes(StandardCharsets.UTF_8)
            );
            producer.send(message, messageQueue);

            // 业务第二条
            message = new Message(
                    "tp_demo_08",
                    ("tp_demo_08 order pay" + i).getBytes(StandardCharsets.UTF_8)
            );
            producer.send(message, messageQueue);

            // 业务第3条
            message = new Message(
                    "tp_demo_08",
                    ("tp_demo_08 order ship" + i).getBytes(StandardCharsets.UTF_8)
            );
            producer.send(message, messageQueue);
        }
        producer.shutdown();
    }
}
